package com.jinxinol.basic.system.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;

import com.jinxinol.basic.system.domain.SysMyInfo;
import com.jinxinol.basic.system.domain.SysUser;
import com.jinxinol.basic.system.service.ISysMyInfoService;
import com.jinxinol.basic.system.service.ISysUserService;
import com.jinxinol.core.common.BaseAction;
import com.jinxinol.core.common.ContextHelper;
import com.jinxinol.core.common.PageModel;
import com.jinxinol.core.util.encryption.MD5;

/**
 * 
 * @company Jinxin Computer Corp.
 * @author Conner
 * @email 764828388@qq.com
 * @description 我的消息记录Action
 * @date 2012-9-10
 */
public class MyMsgAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	private static final Log logger = ContextHelper.getLogger(MyMsgAction.class);
	
	private SysMyInfo sysMyInfo = new SysMyInfo();
	private SysUser sysUser = new SysUser();
	private PageModel<SysMyInfo> pm = new PageModel<SysMyInfo>();
	private List<SysMyInfo> list;
	private List<SysUser> listSysUser;
	private String idStr;
	private String type;
	private Integer id;  //我的消息记录ID
	private int i = 0;
	private int j = 0;
	
	@Resource 
	ISysMyInfoService sysMyInfoService;
	
	@Resource
	ISysUserService sysUserService;
	
	public MyMsgAction(){}
	
	/**
	 * 添加我的消息记录
	 * */
	public String add(){
		try {
			sysMyInfoService.insert(sysMyInfo);
			return "addSuccess";
		} catch (Exception e) {
			logger.error("新增我的消息记录"+sysMyInfo.getbCode()+"失败", e);
			return ERROR;
		}
	}
	
	/**
	 * 删除我的消息记录
	 * */
	public String delete(){
		String[] arr = null;
		try {
			if(!idStr.equals("")){
				arr = idStr.split(",");
				for(int i = 0; i < arr.length ; i++){
					sysMyInfo = new SysMyInfo();
					sysMyInfo.setId(Integer.parseInt(arr[i]));
					sysMyInfoService.delete(sysMyInfo);
				}
				logger.info("删除我的消息记录成功！");
			}
			return "delSuccess";
		} catch (Exception e) {
			logger.error("删除我的消息记录出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 修改我的消息记录
	 * */
	public String update(){
		try {
			sysMyInfoService.update(sysMyInfo);
			return "updSuccess";
		} catch (Exception e) {
			logger.error("修改我的消息记录"+sysMyInfo.getbCode()+"出现异常！", e);
			return ERROR;
		}
	}
	
	/**
	 * 按ID查询我的消息记录
	 * */
	public String queryById(){
		try {
			sysMyInfo = sysMyInfoService.findById(sysMyInfo.getId());
			if(type.equals("detail")){
				return "qurSuccess";
			}else if(type.equals("edit")){
				return "qurSuccess2";
			}else{
				return "qurSuccess";
			}
		} catch (Exception e) {
			logger.error("按ID查询我的消息记录出现异常", e);
			return ERROR;
		}
	}
	
	/**
	 * 按条件查询我的消息记录
	 * */
	public String selectMyMsg(){
		String rname = null;
		try {
			pm = sysMyInfoService.findOnPage(sysMyInfo, pm);
			listSysUser = sysUserService.find(sysUser);
			if(pm.getList().size() > 0 && listSysUser.size() > 0){
				for(i = 0 ; i < pm.getList().size() ; i++){
					for(j = 0 ; j < listSysUser.size() ; j++){
						if(pm.getList().get(i).getUserId() == listSysUser.get(j).getId()){
							rname = listSysUser.get(j).getRname();
							pm.getList().get(i).setRname(rname);
							break;
						}
					}
				}
			}
			return "list";
		} catch (Exception e) {
			logger.error("按条件查询我的消息记录出现异常", e);
			return ERROR;
		}
	}

	//getter & setter
	public SysMyInfo getSysMyInfo() {
		return sysMyInfo;
	}

	public void setSysMyInfo(SysMyInfo sysMyInfo) {
		this.sysMyInfo = sysMyInfo;
	}

	public PageModel<SysMyInfo> getPm() {
		return pm;
	}

	public void setPm(PageModel<SysMyInfo> pm) {
		this.pm = pm;
	}

	public List<SysMyInfo> getList() {
		return list;
	}

	public void setList(List<SysMyInfo> list) {
		this.list = list;
	}

	public String getIdStr() {
		return idStr;
	}

	public void setIdStr(String idStr) {
		this.idStr = idStr;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}
	
}